package com.example.system;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import android.view.View;

public class viewconfig extends View{
	
	private Paint paint;
	private Paint font_paint;
	
	private int numWidth;
	
	private float startHeight = MainActivity.HEIGHT-80;
	
	private float endHeight=startHeight;
	
	private int viewWidth = 20;
	
	private int maxSize = 43;
	private int indexSize = 0;
	
	private String displayMode = "%";
	private boolean mode = false;
	private boolean display = true;
	private boolean animMode = true;
	

	public viewconfig(Context context,int maxSize,String displayMode,boolean mode) {
		super(context);
		// TODO Auto-generated constructor stub
		
		this.maxSize = maxSize;
		this.displayMode=displayMode;
		this.mode=mode;
		init();		
	}
	
	public viewconfig(Context context,int maxSize,String displayMode,boolean mode,boolean animMode){
		super(context);
		this.maxSize = maxSize;
		this.displayMode = displayMode;
		this.mode = mode;
		this.animMode = animMode;
		init();
	}
	
	


	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		canvas.drawRect(10, endHeight,10+viewWidth,startHeight,paint);
		if(!display){
			if(!mode&&indexSize >= 50){
				paint.setARGB(255,200,200,60);
				if(!mode && indexSize >= 80){
					paint.setARGB(255, 
							      (indexSize<100)?(110+indexSize+45):255, 
							      (indexSize<100)?210-(indexSize+45):0, 
							      20);
				}
			}else if(mode&&indexSize<=50){
				paint.setARGB(255, 200, 200, 60);
				if(mode&&indexSize <= 30){
					paint.setARGB(255,255-indexSize,indexSize,20);
				}
			}
			canvas.drawRect(10, endHeight,10+viewWidth,startHeight,paint);
			paint.setARGB(255, 99, 66, 0);
			canvas.drawText(""+indexSize, numWidth, endHeight-5, paint);
			paint.setARGB(255, 110, 210, 60);
		}
		canvas.drawText(displayMode, 0, startHeight+10, font_paint);
	}
	
	
	private void init(){
		
		paint = new Paint();
		paint.setARGB(255, 100, 200, 20);
		font_paint = new Paint();
		font_paint.setARGB(255, 60, 60, 60);
		
		numWidth = 9;
		
		if(maxSize < 10){
			numWidth = 15;
		}else if(maxSize < 100){
			numWidth = 12;
		}
		
		if(animMode){
			thread.start();
		}else{
			display = false;
			indexSize = maxSize;
			endHeight = startHeight - (float)(maxSize*1.5);
			invalidate();
		}
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			super.handleMessage(msg);
			
			if((msg.what==1) && (indexSize < maxSize) && (endHeight>=20)){
				endHeight -= 1.5;
				indexSize += 1; 
			}else{
				display = false;
			}
			invalidate();
		}
	};
	
	
	private Thread thread = new Thread(){
		@Override
		public void run(){
			while(!Thread.currentThread().isInterrupted()&&display)
			{
				Message msg = new Message();
				msg.what=1;
				handler.sendMessage(msg);
				
				try{
					Thread.sleep(20);
				}catch(InterruptedException e){
					Log.e("err", "err Thread close!");
					this.interrupt();
				}
			}
		}
	};
	
}